﻿    using HandyControl.Controls;
    using Infov1.Models;
    using SqlSugar;
    using System.Collections.ObjectModel;
    using System;
    using Infov1.Commands;
    using System.Windows.Input;
    using System.Windows;  // 添加 Window 引用
    using System.Linq;    // 添加 LINQ 引用
    using Window = System.Windows.Window;  // 明确使用 System.Windows.Window

    namespace Infov1.ViewModels
    {
        public class CustomerEditViewModel : ViewModelBase
        {
            private readonly ISqlSugarClient _db;
            private readonly bool _isNewMode;
            private string _maxCurCode;
            private CustomerInfoModel _customer;
            private ObservableCollection<CustomerCategoryModel> _categories;
            private ObservableCollection<CustomerAreaModel> _areas;

            public CustomerEditViewModel(CustomerInfoModel customer, bool isNewMode)
            {
                _db = DbContext.GetConnection();
                _isNewMode = isNewMode;
                // 获取最大的 customer_code
                MaxCurCode = _db.Queryable<CustomerInfoModel>().Max(x => x.CustomerCode);
                
                Customer = customer ?? new CustomerInfoModel();
                
                // 初始化命令
                SaveCommand = new RelayCommand(Save);
                CancelCommand = new RelayCommand(Cancel);

                LoadCategories();
                LoadAreas();
            }

            // 客户信息
            public CustomerInfoModel Customer
            {
                get => _customer;
                set
                {
                    _customer = value;
                    OnPropertyChanged(nameof(Customer));
                }
            }
            //最大编码值
            public string MaxCurCode
            {
                get=> _maxCurCode;
                set
                {
                    _maxCurCode = value;
                    OnPropertyChanged(nameof(MaxCurCode));
                }
            }
            public ObservableCollection<CustomerCategoryModel> Categories
            {
                get => _categories;
                set
                {
                    _categories = value;
                    OnPropertyChanged(nameof(Categories));
                }
            }
            //区域
            public ObservableCollection<CustomerAreaModel> Areas
            {
                get => _areas;
                set
                {
                    _areas = value;
                    OnPropertyChanged(nameof(Areas));
                }
            }

            public ICommand SaveCommand { get; }
            public ICommand CancelCommand { get; }

          // 添加加载方法
            private void LoadCategories()
            {
                try
                {
                    var list = _db.Queryable<CustomerCategoryModel>()
                        .OrderBy(x => x.CategoryCode)
                        .ToList();
                    Categories = new ObservableCollection<CustomerCategoryModel>(list);
                }
                catch (Exception ex)
                {
                    Growl.Error($"加载类别失败：{ex.Message}");
                }
            }
         //加载区域
            private void LoadAreas()
            {
                try
                {
                    var list = _db.Queryable<CustomerAreaModel>()
                        .OrderBy(x => x.AreaCode)
                        .ToList();
                    Areas = new ObservableCollection<CustomerAreaModel>(list);
                }
                catch (Exception ex)
                {
                    Growl.Error($"加载区域失败：{ex.Message}");
                }
            }
         //保存
            private void Save()
            {
                if (!ValidateData()) return;
                if (Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.DataContext == this) is Window window)
                {
                    window.DialogResult = true;

                }
            }

            private void Cancel()
            {
                if (Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.DataContext == this) is Window window)
                {
                    window.DialogResult = false;
                    window.Close();
                }
            }

            private bool ValidateData()
            {
                if (string.IsNullOrWhiteSpace(Customer.CustomerCode))
                {
                    Growl.Warning("请输入客户编码！");
                    return false;
                }

                if (Customer.CustomerCode.Length != 6)
                {
                    Growl.Warning("客户编码必须为6位！");
                    return false;
                }

                if (string.IsNullOrWhiteSpace(Customer.CompanyName))
                {
                    Growl.Warning("请输入公司名称！");
                    return false;
                }

                if (string.IsNullOrWhiteSpace(Customer.CategoryCode))
                {
                    Growl.Warning("请选择所属类别！");
                    return false;
                }

                if (string.IsNullOrWhiteSpace(Customer.AreaCode))
                {
                    Growl.Warning("请选择所属区域！");
                    return false;
                }

                return true;
            }
        }
    }